package com.token.sysflow.hander;

import com.token.sysflow.bean.FlowFlowInfo;
import com.token.sysflow.bean.FlowStepBasic;
import com.token.sysflow.enumerate.StepTypeEnum;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sql.IntegrateSql;
import lombok.Getter;

import java.sql.SQLException;
import java.util.Vector;

/**
 * 流程
 */
public class Flow extends FlowFlowInfo{
    private FlowStepBasic stepBasic;
    private final IntegrateSql iSql;

    @Getter
    private final boolean isHas;
    public Flow(IntegrateSql iSql,String flowId) throws Exception {
        super(iSql,flowId);
        this.isHas = this.isInitRs();
        this.iSql = iSql;
    }

    /**
     * 获取起草环节
     * @return Step
     * @throws Exception 抛出错误
     */
    public Step getStartStep() throws Exception{
        stepBasic = new FlowStepBasic(iSql);
        Vector<String> vec = new Vector<>(2);
        vec.add(this.getFlowId());
        vec.add(StepTypeEnum.STEPTYPE_START.getTypeKey());
        stepBasic.initParamToRs("FlowId=? and StepType=?",vec);

        if(!stepBasic.isInitRs()){
            throw new FlowException(ErrorCodeEnum.STEPSTART_NOFIND_ERROR);
        }

        Step step = new Step(iSql,stepBasic.getStepId());
        stepBasic.close();
        return step;
    }
    /**
     * 是否激活流程
     * @return
     */
    public boolean isActive(){
        return "1".equals(this.getIsActive());
    }


    /**
     * 检查起草权限
     * @return true 有权限
     */
    public boolean checkDraftPower(){
        // TODO 检查起草权限
        return true;
    }

    /**
     *
     */
    public void close(){
        try {
            super.close();
            if(stepBasic!=null) stepBasic.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

}